package com.ujoku.controller.admin;

import com.labillusion.core.platform.cdn.upyum.UpYun;
import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.util.ImageUtils;
import com.rits.cloning.Cloner;
import com.ujoku.domain.Winery;
import com.ujoku.service.WineryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Greg.Chen on 14-11-12.
 */
@RestController
public class WineryMngController extends RESTController {

    @Autowired
    private WineryService wineryService;

    @Autowired @Qualifier("cloner")
    private Cloner cloner;

    @Autowired
    private UpYun upyun;

    @Value("#{configProperties['settings.winery.upload']}")
    private String uploadPath;

    @Value("#{configProperties['settings.cdn.winery.upload']}")
    private String cdnUploadPath;

    @RequestMapping(value = "/uj-admin/winery/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Winery> list(){

        List<Winery> wineryList = wineryService.selectList();

        return wineryList;
    }

    @RequestMapping(value = "/uj-admin/winery/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Winery getById(@PathVariable String id){
        return (Winery)wineryService.selectById(id);
    }

    @RequestMapping(value = "/uj-admin/winery/add", method = RequestMethod.POST)
    @ResponseBody
    public Winery add(@RequestBody Winery winery){
        wineryService.insert(winery);
        return winery;
    }

    @RequestMapping(value = "/uj-admin/winery", method = RequestMethod.PUT)
    @ResponseBody
    public Winery update(@RequestBody Winery winery){
        wineryService.update(winery);
        return winery;
    }

    @RequestMapping(value = "/uj-admin/winery/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable int id, HttpServletRequest request) throws IOException {
        wineryService.deleteById(id);

        String dir = request.getRealPath(uploadPath);
        String path = dir+"/"+ id + ".jpg";

        File  localFile = new File(path);
        upyun.deleteFile(cdnUploadPath + localFile.getName());

        return "successfully";
    }

    @RequestMapping(value="/uj-admin/winery/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(  @RequestParam MultipartFile file,
                           @RequestParam int id,
                           @RequestParam int index , HttpServletRequest request) throws IOException {

        Winery winery =  (Winery)wineryService.selectById(id);
        //更新时间戳
        wineryService.update(winery);

        String dir = request.getRealPath(uploadPath);
        File folder = new File(dir);
        if(!folder.exists()){
            folder.mkdirs();
        }

        String originalPath = dir+"/"+ winery.getId() + "_"+ index +".jpg";
        File originalFile = new File(originalPath);
        //将上传文件写入到指定文件出
        file.transferTo(originalFile);

        String path = dir+"/"+ winery.getId() + "_"+ index +".jpg";

        File  localFile = new File(path);
        upyun.setContentMD5(UpYun.md5(localFile));
        upyun.writeFile(cdnUploadPath + localFile.getName(), localFile, true);

        return "Successfully";
    }
}
